<?php


namespace app\common\model;

class Collection extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_collection';
    // 主键ID
    protected $pk = 'c_id';

	public function record($user_id,$product_id){
		$where = ['user_id'=>$user_id,'product_id'=>$product_id];
		$info = $this
			->field($this->pk)
			->where($where)
			->find();

		if($info){
			$res = $this->where($where)->delete();
		}else{
			$res = $this->save([
				'user_id'=>$user_id,
				'product_id'=>$product_id,
				'add_time'=>time(),
			]);
		}
		return $res;
	}

	/**
	 * @param $user_id
	 * @param $product_ids
	 * @return bool
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function Dels($user_id,$product_ids){
		$res = $this
			->where([
				['user_id','=',$user_id],
				['product_id','in',$product_ids],
			])
			->delete();

		return $res;
	}

	function GetMyCollection($user_id, $limit)
	{
		$field = ['p.product_id','p.product_name','p.price','p.mprice','p.shop_id','p.imageurl'];
		$data = $this
			->alias('c')
			->field($field)
			->where(['c.user_id'=>$user_id,'is_del'=>1])
			->leftJoin([
				['ddt_products p','c.product_id=p.product_id'],
			])
			->order('c.add_time desc')
			->paginate($limit);
		return $data;
	}

    function get_list($field='*',$cond=null, $orderby='sort asc')
    {
		$where = null;

		if(!empty($cond['user_id'])){
			$where[] = ['user_id','=', $cond['user_id']];
		}

        $rs = $this
            ->field($field)
            ->where($where)
            ->order($orderby)
            ->select();
        return $rs;
    }

}